// LoadSpeedView.cpp : implementation of the CLoadSpeedView class
//

#include "stdafx.h"
#include "LoadSpeed.h"

#include "LoadSpeedDoc.h"
#include "LoadSpeedView.h"
#include "MainFrm.h"

#include "QPerformanceTimer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView

IMPLEMENT_DYNCREATE(CLoadSpeedView, CView)

BEGIN_MESSAGE_MAP(CLoadSpeedView, CView)
	//{{AFX_MSG_MAP(CLoadSpeedView)
	ON_COMMAND(ID_CREATE_RAND_POINTS_TOOLBAR, OnCreateRandPointsToolbar)
	ON_CBN_SELENDOK(ID_LOAD_STYLE_COMBOX, OnLoadStyleCombox)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView construction/destruction

CLoadSpeedView::CLoadSpeedView():nLoadParseStyle(FILE_PARSE_MFC),m_nTimer(0)
{
	// TODO: add construction code here
	pFPData = new CFPData();
}

CLoadSpeedView::~CLoadSpeedView()
{
	if(pFPData != NULL)
		delete pFPData;
	pFPData = NULL;
}

BOOL CLoadSpeedView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView drawing

void CLoadSpeedView::OnDraw(CDC* pDC)
{
	CLoadSpeedDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	pFPData->DrawVertexs(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView printing

BOOL CLoadSpeedView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLoadSpeedView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLoadSpeedView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView diagnostics

#ifdef _DEBUG
void CLoadSpeedView::AssertValid() const
{
	CView::AssertValid();
}

void CLoadSpeedView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLoadSpeedDoc* CLoadSpeedView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLoadSpeedDoc)));
	return (CLoadSpeedDoc*)m_pDocument;
}
#endif //_DEBUG

BOOL CLoadSpeedView::LoadFile(LPCSTR strFileName)
{
	BOOL bret;
	{
		QPerformanceTimer timer(m_nTimer);
		bret = pFPData->LoadData(strFileName,nLoadParseStyle);
	}
	CString str;
	switch(nLoadParseStyle) {
	case FILE_PARSE_MFC:
		str.Format("MFC Load Timing:%dms",m_nTimer);
		break;
	case FILE_PARSE_STL:
		str.Format("STL Load Timing:%dms",m_nTimer);
		break;
	case FILE_PARSE_MAP:
		str.Format("MAP Load Timing:%dms",m_nTimer);
		break;
	default:
		break;
	}
	SetStatusBarText(1,LPCSTR(str));
	return bret;
}

BOOL CLoadSpeedView::SaveFile(LPCSTR strFileName)
{
	BOOL bret;
	{
		QPerformanceTimer timer(m_nTimer);
		bret = pFPData->SaveData(strFileName,nLoadParseStyle);
	}
	CString str;
	switch(nLoadParseStyle) {
	case FILE_PARSE_MFC:
		str.Format("MFC Save Timing:%dms",m_nTimer);
		break;
	case FILE_PARSE_STL:
		str.Format("STL Save Timing:%dms",m_nTimer);
		break;
	case FILE_PARSE_MAP:
		str.Format("MAP Save Timing:%dms",m_nTimer);
		break;
	default:
		break;
	}
	SetStatusBarText(1,LPCSTR(str));
	return bret;
}

BOOL CLoadSpeedView::SetStatusBarText(int nID, LPCSTR strText)
{
	CStatusBar* pStatus = (CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
	
	if(pStatus)
		return pStatus->SetPaneText(nID,strText);
	else
		return FALSE;
	
	return TRUE;
}

////////////////////////////////////////////////////////////////////////////
// CLoadSpeedView message handlers

void CLoadSpeedView::OnCreateRandPointsToolbar() 
{
	// TODO: Add your command handler code here
	CRect rect;
	GetClientRect(&rect);
	pFPData->CreatRandData(20000,rect);
	RedrawWindow();
}

void CLoadSpeedView::OnLoadStyleCombox()
{
	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
	nLoadParseStyle = pMainWnd->GetLoadStyle();
}
